package hng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import hng.dto.CartDTO;
import hng.exception.BizException;
import hng.interceptor.LoginInterceptor;
import hng.model.CartDO;
import hng.mapper.CartMapper;
import hng.model.LoginUser;
import hng.service.CartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hng.vo.CartProductVO;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 购物车表 服务实现类
 * </p>
 *
 * @author 告白
 * @since 2024-06-27
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, CartDO> implements CartService {

    @Autowired(required = false)
    private CartMapper cartMapper;

    /**
     * 添加商品到购物车
     */
    @Override
    public void add(CartDTO cartDTO) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        if (cartDTO.getQuantity() <= 0) {
            throw new BizException("数量不能小于1");
        }

        CartDO cartDO = cartMapper.selectOne(new LambdaQueryWrapper<CartDO>()
                .eq(CartDO::getUserId, loginUser.getId())
                .eq(CartDO::getProductId, cartDTO.getProductId())
                .eq(CartDO::getSpecId, cartDTO.getSpecId())
        );
        // 该商品的规格已在购物车中，只要修改数量
        if (cartDO != null) {
            cartDO.setQuantity(cartDO.getQuantity() + cartDTO.getQuantity());
            cartMapper.updateById(cartDO);
            return;
        }
        // 不在则新增
        CartDO cartDO1 = new CartDO();
        BeanUtils.copyProperties(cartDTO, cartDO1);
        cartDO1.setUserId(loginUser.getId());
        cartMapper.insert(cartDO1);
    }

    /**
     * 删除购物车的商品
     */
    @Override
    public void del(Integer cartId) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        int row = cartMapper.delete(new LambdaQueryWrapper<CartDO>()
                .eq(CartDO::getUserId, loginUser.getId())
                .eq(CartDO::getId, cartId)
        );
    }

    /**
     * 修改购物车的商品
     */
    @Override
    public void update(CartDTO cartDTO) {
        if (cartDTO.getQuantity() <= 0) {
            throw new BizException("数量不能小于1");
        }

        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        CartDO cartDO = new CartDO();
        BeanUtils.copyProperties(cartDTO, cartDO);
        cartDO.setUserId(loginUser.getId());
        int row = cartMapper.update(cartDO, new LambdaQueryWrapper<CartDO>()
                .eq(CartDO::getUserId, loginUser.getId())
                .eq(CartDO::getId, cartDTO.getId())
        );
    }

    /**
     * 查询购物车里的商品列表
     */
    @Override
    public List<CartProductVO> productList() {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        List<CartProductVO> list = cartMapper.getList(loginUser.getId());
        return list;
    }

}
